        var httpObject = null;
        var link = "";
        var link2 = "";
        var link3 = "";
        var pinglink = "";
        var ScrollDown = 0;
        var msgBox = document.getElementById('msgs');
        var timerID = 0;

        // Get the HTTP Object
        function getHTTPObject() {
            if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
            else if (window.XMLHttpRequest) return new XMLHttpRequest();
            else {
                alert("Your browser does not support AJAX.");
                return null;
            }
        }


        // MSGBOX input
        function UserPM(tag) {
            var msgInput = document.getElementById('msg');
            var content = msgInput.value;
            msgInput.value = '/msg ' + tag + ' ' + content;
        }

        // Change the value of the outputText field
        function setHtml() {
            if(ajaxVar.readyState == 4){
                var response = ajaxVar.responseText;
                var msgBox = document.getElementById("msgs");
                msgBox.innerHTML += response;
                ScrollDown = 0;
                MsgScrollDown();
            }
        }

        // Change the value of the outputText field
        function setAll() {
            if(ajaxVar.readyState == 4){
                var response = ajaxVar.responseText;
                var msgBox = document.getElementById("msgs");
                msgBox.innerHTML = response;
                ScrollDown = 0;
                MsgScrollDown();
            }
        }

        // Getting and generating user list.
        function genUserlist() {
            if(ajaxUserlist.readyState == 4){
                var response = ajaxUserlist.responseText;
                var userlist = document.getElementById('userlist');
                userlist.innerHTML = response;
            }
        }

        function getUserlist() {
            ajaxUserlist = getHTTPObject();
            //var randomnumber=Math.floor(Math.random()*10000);
            if (ajaxUserlist != null) {
                link = "server.php?userlist&nick="+nickName;
                ajaxUserlist.open("GET", link , true);
                ajaxUserlist.onreadystatechange = genUserlist;
                ajaxUserlist.send(null);
            }
        }

        

        function doNotificationPm() {
            if(ajaxVar4.readyState == 4){
                var mentionUser = ajaxVar4.responseText;
                if(Notification.permission==="granted") {
                    var notify = new Notification("Private message from " + mentionUser, { body: ""});
                }
            }
        }

        function getPmUser() {
            ajaxVar4 = getHTTPObject();
            //var randomnumber=Math.floor(Math.random()*10000);
            if (ajaxVar4 != null) {
                link4 = "server.php?get=notificationpmed&nick="+nickName;
                ajaxVar4.open("GET", link4 , true);
                ajaxVar4.onreadystatechange = doNotificationPm;
                ajaxVar4.send(null);
            }
        }

        function checkNotificationPm() {
            if(ajaxVar5.readyState == 4){
                var returnMsg = ajaxVar5.responseText;
                var compareString = "true";
                //console.log(returnMsg);
                if(returnMsg.trim() === compareString.trim()) {
                    getPmUser();
                }
            }
        }

        function checkPmUrl() {
            ajaxVar5 = getHTTPObject();
            //var randomnumber=Math.floor(Math.random()*10000);
            if (ajaxVar5 != null) {
                link5 = "server.php?get=notificationpmedexists&nick="+nickName;
                ajaxVar5.open("GET", link5 , true);
                ajaxVar5.onreadystatechange = checkNotificationPm;
                ajaxVar5.send(null);
            }
        }


        function doNotificationMention() {
            if(ajaxVar2.readyState == 4){
                var mentionUser = ajaxVar2.responseText;
                if(Notification.permission==="granted") {
                    var notify = new Notification("You were mentioned by " + mentionUser, { body: ""});
                }
            }
        }

        function getMentionUser() {
            ajaxVar2 = getHTTPObject();
            //var randomnumber=Math.floor(Math.random()*10000);
            if (ajaxVar2 != null) {
                link2 = "server.php?get=notificationmention&nick="+nickName;
                ajaxVar2.open("GET", link2 , true);
                ajaxVar2.onreadystatechange = doNotificationMention;
                ajaxVar2.send(null);
            }
        }

        function checkNotificationMention() {
            if(ajaxVar3.readyState == 4){
                var returnMsg = ajaxVar3.responseText;
                var compareString = "true";
                //console.log(returnMsg);
                if(returnMsg.trim() === compareString.trim()) {
                    getMentionUser();
                }
            }
        }

        function checkMentionUrl() {
            ajaxVar3 = getHTTPObject();
            //var randomnumber=Math.floor(Math.random()*10000);
            if (ajaxVar3 != null) {
                link3 = "server.php?get=notificationmentionexists&nick="+nickName;
                ajaxVar3.open("GET", link3 , true);
                ajaxVar3.onreadystatechange = checkNotificationMention;
                ajaxVar3.send(null);
            }
        }


         // Implement business logic
        function serverWrite() {
            ajaxVar = getHTTPObject();
            if (ajaxVar != null) {
                link = "server.php?nick="+nickName+"&msg="+encodeURIComponent(document.getElementById('msg').value);
                ajaxVar.open("GET", link , true);
                ajaxVar.send(null);
            }
        }

        // Implement business logic
        function serverReload() {
            ajaxVar = getHTTPObject();
            //var randomnumber=Math.floor(Math.random()*10000);
            if (ajaxVar != null) {
                link = "server.php?get&nick="+nickName;
                ajaxVar.open("GET", link , true);
                ajaxVar.onreadystatechange = setAll;
                ajaxVar.send(null);
            }
        }

        function MsgScrollDown() {
            if(ScrollDown != 1) {
                var msgBox = document.getElementById("msgs");
                msgBox.scrollTop = msgBox.scrollHeight;
                ScrollDown = 1;
            }
        }

        function UpdateTimer() {
            serverReload();
            MsgScrollDown();
            getUserlist();
            checkPmUrl();
            checkMentionUrl();
            setTimeout(UpdateTimer, 1000);
        }

        function doLogin() {
            ajaxVar = getHTTPObject();
            if(ajaxVar != null) {
                link = "server.php?do=login&nick="+nickName;
                ajaxVar.open("GET", link, true);
                ajaxVar.onreadystatechange = setHtml;
                ajaxVar.send(null);
                setTimeout(function() { window.location.reload(); },1000);
            }
        }

        function doClearLog() {
            ajaxVar = getHTTPObject();
            if(ajaxVar != null) {
                link = "server.php?do=clearlog&nick="+nickName;
                ajaxVar.open("GET", link, true);
                ajaxVar.onreadystatechange = setHtml;
                ajaxVar.send(null);
                setTimeout(function() { window.location.reload(); },1000);
            }
        }

        function doLogout() {
            ajaxVar = getHTTPObject();
            if(ajaxVar != null) {
                link = "server.php?do=logout&nick="+nickName;
                ajaxVar.open("GET", link, true);
                ajaxVar.onreadystatechange = setHtml;
                ajaxVar.send(null);
                setTimeout(function() { window.location.reload(); },1000);
            }
        }

        function wrapBBCode(tag) {
            var msgInput = document.getElementById('msg');
            var content = msgInput.value;
            var selectedContent = content.substring(msgInput.selectionStart, msgInput.selectionEnd);
            var beforeContent = content.substring(0, msgInput.selectionStart);
            var afterContent = content.substring(msgInput.selectionEnd, content.length);
            msgInput.value = beforeContent + '[' + tag + ']' + selectedContent + '[/' + tag + ']' + afterContent;
        }
